package action.ManagerAction;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import common.Pagination;
import entity.CroCoupons;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroCouponsService;
import service.CroCustomerService;
import vo.ShowCouponsVo;


import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/5/8 0008.
 */
public class CroCouponsAction extends ActionSupport{
    private ShowCouponsVo couponsVo;
    private ShowCouponsVo recouponsVo;
    private List<CroCoupons> croCoupons;
    private CroCoupons upcroCoupons;
    private String id;
    private double uplimit;
    private String successResult;
    private double value;
    private String description;
    private String type;
    private Date offdate;
    private String userid;
    private String status;
    private int number;
    private int pageIndex;
    private int pageCount;
    private int pageSize = 10;
    private String jsonObject;


    @Autowired
    private CroCouponsService croCouponsService;



    /*
   * 多属性搜索
   * */
    @RequiresPermissions("user:select")
    public String querylist(){
        Pagination<CroCoupons> page;
        if(pageIndex==0){
            page=croCouponsService.FindUserByProperty(couponsVo,1,pageSize);
        }else {
            page=croCouponsService.FindUserByProperty(couponsVo,pageIndex,pageSize);
        }
        setPageCount((int) page.getPagesCount());
        setPageIndex((int) page.getCurIndex());
        setRecouponsVo(couponsVo);
        jsonObject= JSON.toJSONString(recouponsVo);
        setCroCoupons(page.getItems());
        return SUCCESS;
    }
    /*
   * 分页展示优惠券信息
   * */
    @RequiresPermissions("user:select")
    public String showdetail(){
        Pagination<CroCoupons> page;
        if(pageIndex==0){
            page=croCouponsService.findAllUser(1,pageSize);
        }else{
            page=croCouponsService.findAllUser(pageIndex,pageSize);
        }
        setPageCount((int) page.getPagesCount());
        setPageIndex((int) page.getCurIndex());
        setCroCoupons(page.getItems());
        return SUCCESS;
    }


    /*删除*/
    @RequiresPermissions("user:delete")
    public String deleteCoupons(){
        if(croCouponsService.deleteCoupons(id)){
            successResult="删除成功";
        }else {
            successResult="删除失败";
        }
        return SUCCESS;
    }


    @RequiresPermissions("user:write")
    /*添加操作*/
    public String addCoupons(){
        CroCoupons croCoupons = new CroCoupons();
        croCoupons.setType(type);
        croCoupons.setDescription(description);
        croCoupons.setValue(value);
        croCoupons.setUplimit(uplimit);
        if(croCouponsService.addCoupons(croCoupons)){
            successResult="修改成功";
        }else {
            successResult="修改失败";
        }
        return SUCCESS;
    }

    @RequiresPermissions("user:update")
    /*发放优惠券*/
    public String issueCoupons(){
        CroCoupons croCoupons=croCouponsService.FindCouponsByid(id);
        String status="1";
        croCoupons.setOffdate(offdate);
        croCoupons.setStatus(status);
        croCoupons.setNumber(number+croCoupons.getNumber());
        if(croCouponsService.issueCoupons(croCoupons)){
            successResult="修改成功";
        }else {
            successResult="修改失败";
        }
        return SUCCESS;
    }

    @RequiresPermissions("user:update")
    /*修改操作*/
    public String updateCoupons(){
        CroCoupons croCoupons=croCouponsService.FindCouponsByid(id);
        croCoupons.setType(type);
        croCoupons.setDescription(description);
        croCoupons.setValue(value);
        croCoupons.setUplimit(uplimit);
        croCoupons.setStatus(status);
        croCoupons.setOffdate(offdate);
        if(croCouponsService.issueCoupons(croCoupons)){
            successResult="修改成功";
        }else {
            successResult="修改失败";
        }
        return SUCCESS;
    }

    /*根据id查找信息*/
    @RequiresPermissions("user:select")
    public String listCoupons(){
        upcroCoupons=croCouponsService.FindCouponsByid(id);
        return "updatecoupons";
    }


    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public CroCoupons getUpcroCoupons() {
        return upcroCoupons;
    }

    public void setUpcroCoupons(CroCoupons upcroCoupons) {
        this.upcroCoupons = upcroCoupons;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public ShowCouponsVo getCouponsVo() {
        return couponsVo;
    }

    public void setCouponsVo(ShowCouponsVo couponsVo) {
        this.couponsVo = couponsVo;
    }

    public ShowCouponsVo getRecouponsVo() {
        return recouponsVo;
    }

    public void setRecouponsVo(ShowCouponsVo recouponsVo) {
        this.recouponsVo = recouponsVo;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getJsonObject() {
        return jsonObject;
    }

    public void setJsonObject(String jsonObject) {
        this.jsonObject = jsonObject;
    }

    public List<CroCoupons> getCroCoupons() {
        return croCoupons;
    }

    public void setCroCoupons(List<CroCoupons> croCoupons) {
        this.croCoupons = croCoupons;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public Date getOffdate() {
        return offdate;
    }

    public void setOffdate(Date offdate) {
        this.offdate = offdate;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getUplimit() {
        return uplimit;
    }

    public void setUplimit(double uplimit) {
        this.uplimit = uplimit;
    }

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public CroCouponsService getCroCouponsService() {
        return croCouponsService;
    }

    public void setCroCouponsService(CroCouponsService croCouponsService) {
        this.croCouponsService = croCouponsService;
    }
}
